<?php
/**
 * RadioMorningController.class.php
 * 晨间电台控制器
 * 
 * @author 	王中艺	<wangzy_smile@qq.com>
 * @date 	2017-05-04
 */

namespace Wechat\Controller;

class RadioMorningController extends BaseController {

	function index(){
		$userInfo 	= C('User');

		//自动执行签到
		D('Check')->checkToday($userInfo['id']);

		//声音列表获取
		$radioList 	= [];
		$tryList 	= D('RadioMorning')->getList(['is_try' => 1]);
		if($userInfo['vip_level'] != 10 && $userInfo['vip_level'] > 0){
			$radioList 	= D('RadioMorning')->getList([
				'is_try' => 0,
				'status'	=> 1
			]);
			$actionRadio 	= $radioList[0];
		}else{
			//当前试听课获取
			$actionRadio 	= D('RadioMorning')->getFreeItem($userInfo['id']);
		}
		
		//消除当前活动电台以获取往期声音
		if($userInfo['vip_level'] != 10){
			array_shift($radioList);
		}else{
			array_shift($tryList);
		}

		//排名信息获取
		$myLisInfo 	= C('user.id');
		$rankList 	= D('RadioMorning')->getRankList(10, $myLisInfo);
		$userInfo['lis_info']	= $myLisInfo;

		//弹窗展示
		$modalShow 	= date('w') == 1 && !S('modal_show_'.$userInfo['id']) ? 1 : 0;
		if($modalShow){
			S('modal_show_'.$userInfo['id'], 1, 3600 * 24);
		}

		//签到信息获取
		$checkInfo 	= D('Check')->getLastInfo($userInfo['id']);
		$checkInfo['format_keep_days']	= sprintf('%03d', $checkInfo['keep_days']);

		//响应信息
		$this->response([
			'user_info'	=> $userInfo,
			'radio_list'	=> $radioList,
			'try_list'	=> $tryList,
			'action_radio'	=> $actionRadio,
			'rank_list'	=> $rankList,
			'listen_count'	=> get_online_count(),
			'check_info'	=> $checkInfo,
			'modal_show'	=> 0,//$modalShow,
			'parent_will'	=> D('PlatInfo')->getItem('MORNING_PARENT_WILL')
		]);
	}

	function getList(){
		$page 	= $this->request('p', 1);
		$condition 	= $this->request('condition', []);
		$radioList 	= D('RadioMorning')->getList($condition, $page);

		$this->response(['list' => $radioList]);
	}

	function logAction(){
		$this->unEmptyParam(['radio_id']);

		$radioId 	= $this->iInfo['radio_id'];
		$userId 	= C('user.id');

		$model 	= new \Think\Model();
		$model->startTrans();

		$result 	= D('RadioMorning')->logAction($radioId, $userId);
		if($result === false){
			$model->rollback();
			$this->outOInfo(D('RadioMorning')->errMsg, 0);
		}
		$result 	= D('Action')->addItem($userId, 3, $radioId);
		if($result === false){
			$model->rollback();
			$this->outOInfo(D('Action')->errMsg, 0);
		}

		$model->commit();
		$this->outOInfo('Oh yes!');
	}

	/**
	 * 检测是否可进入社区
	 * @return 	boolean
	 */
	function showBBS(){
		$this->response();
	}
}
